﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using PPI.Client.Logic.XML;
using PPI.Entities;
using PPI.Entities.Interface;
using PPI.Entities.XML;
using VMind.Global;

namespace PPI.Client.Logic.Upload
{
    public class LUploadStrategyGPSData : LUploadStrategyBase
    {

        public LUploadStrategyGPSData(EXmlOutboxBase database)
        :base(database)
        {
        }

        public override void StartWork(BackgroundWorker worker, DoWorkEventArgs workerEvents)
        {
            StringBuilder _errorMessage = new StringBuilder();
            Int32 _TotalToUpload = 0;
            workerEvents.Result = "";

            bool _FoundedProblems = false;
            List<EVendor_Visit_XMissionStructure> m_VendorVisits =
                LVendor_Visit_XMissionStructure.GetMobileVisits(m_OutboxDatabase.ConnectionString);

            foreach (EVendor_Visit_XMissionStructure v in m_VendorVisits)
            {
                string message = "";
                if (LVendor_Visit_XMissionStructure.CheckIsValidVisit(v, ref message))
                {
                    _errorMessage.AppendLine(message);
                    _FoundedProblems = true;
                }
            }
            if (!_FoundedProblems)
            {
                LXmlOutbox _serviceLogic = new LXmlOutbox(m_OutboxDatabase);
                m_OutboxDatabase.IsOpened = true;
                _serviceLogic.UpdateNode(m_OutboxDatabase);

                _TotalToUpload += m_VendorVisits.Count;
                foreach (EVendor_Visit_XMissionStructure Visit in m_VendorVisits)
                {
                    _TotalToUpload += Visit.Poles.Count;
                }
                Int32 _CountUploaded = _TotalToUpload;

                Int32 _NewStructureServerID;
                Int32 _NewPoleServerID;

                foreach (EVendor_Visit_XMissionStructure Visit in m_VendorVisits)
                {
                    if (Visit.ServerID == Int32.MinValue)
                    {
                        Int32 mobileId = Visit.Id;
                        Visit.Id = Visit.ServerID;
                        _NewStructureServerID = LVendor_Visit_XMissionStructure.Save(Visit);

                        Visit.ServerID = _NewStructureServerID;
                        Visit.Id = mobileId;
                        if (_NewStructureServerID > 0)
                            LVendor_Visit_XMissionStructure.UpdateUploadedData(Visit, m_OutboxDatabase.ConnectionString);
                    }
                    else
                    {
                        _NewStructureServerID = Visit.ServerID;
                    }
                    if (worker.CancellationPending)
                    {
                        workerEvents.Cancel = true;
                        return;
                    }
                    foreach (EVendor_Visit_XMissionStructures_Pole Pole in Visit.Poles)
                    {
                        if (Pole.ServerID == Int32.MinValue && _NewStructureServerID > 0)
                        {
                            Int32 mobilePoleId = Pole.Id;
                            Pole.Id = Pole.ServerID;
                            Pole.Asset.Id = _NewStructureServerID;
                            _NewPoleServerID = LVendor_Visit_XMissionStructures_Pole.Save(Pole);
                            Pole.ServerID = _NewPoleServerID;
                            Pole.Id = mobilePoleId;
                            if (_NewPoleServerID > 0)
                                LVendor_Visit_XMissionStructures_Pole.UpdateUploadedData(Pole, m_OutboxDatabase.ConnectionString);
                            System.Threading.Thread.Sleep(200);
                        }
                        _CountUploaded--;
                        worker.ReportProgress(ReportProgress(_CountUploaded, _TotalToUpload));
                        if (worker.CancellationPending)
                        {
                            workerEvents.Cancel = true;
                            return;
                        }
                    }
                    _CountUploaded--;
                    worker.ReportProgress(ReportProgress(_CountUploaded, _TotalToUpload));
                    System.Threading.Thread.Sleep(300);
                }
            }
            else
            {
                throw new Exception(_errorMessage.ToString());
            }
        }

        public override void FinishWork()
        {
            LXmlOutbox _serviceLogic = new LXmlOutbox(m_OutboxDatabase);
            m_OutboxDatabase.IsDataUploaded = true;
            m_OutboxDatabase.DataFinishedToUpload = DateTime.Now;
            _serviceLogic.UpdateNode(m_OutboxDatabase);
        }

        
    }
}
